package com.weixin.engine.daily.dingding;

import java.util.ArrayList;
import java.util.List;

// 审批状态
enum ApprovalStatus {
    APPROVED,   // 已批准
    REJECTED,   // 已拒绝
    PENDING,    // 待审批
    FORWARDED   // 已转发
}

// 审批单
class ApprovalRequest {
    private String content; // 审批内容
    private ApprovalStatus status; // 审批状态

    public ApprovalRequest(String content) {
        this.content = content;
        this.status = ApprovalStatus.PENDING; // 默认为待审批状态
    }

    public String getContent() {
        return content;
    }

    public ApprovalStatus getStatus() {
        return status;
    }

    public void setStatus(ApprovalStatus status) {
        this.status = status;
    }
}

// 审批人接口
interface Approver {
    void process(ApprovalRequest request);
}

// 具体的审批人
class Manager implements Approver {
    @Override
    public void process(ApprovalRequest request) {
        // 模拟经理审批逻辑
        // 实际逻辑可能包括对审批单内容的审核、决策等
        request.setStatus(ApprovalStatus.APPROVED); // 经理审批后直接批准
    }
}

class Supervisor implements Approver {
    @Override
    public void process(ApprovalRequest request) {
        // 模拟主管审批逻辑
        // 实际逻辑可能包括对审批单内容的审核、决策等
        request.setStatus(ApprovalStatus.FORWARDED); // 主管审批后转发给其他审批人
    }
}

// 审批流程
class ApprovalProcess {
    private List<Approver> approvers = new ArrayList<>(); // 审批人列表

    public void addApprover(Approver approver) {
        approvers.add(approver);
    }

    public void submitApprovalRequest(ApprovalRequest request) {
        // 遍历所有审批人
        for (Approver approver : approvers) {
            approver.process(request); // 让审批人处理审批请求
            if (request.getStatus() != ApprovalStatus.PENDING) { // 如果审批状态不再是待审批状态，结束循环
                break;
            }
        }
    }
}

// 审批流程管理器（观察者）
class ApprovalProcessManager {
    private List<ApprovalProcess> processes = new ArrayList<>(); // 审批流程列表

    public void addApprovalProcess(ApprovalProcess process) {
        processes.add(process);
    }

    public void executeApprovalProcesses() {
        // 遍历所有审批流程
        for (ApprovalProcess process : processes) {
            // 模拟审批单的提交
            ApprovalRequest request = new ApprovalRequest("Sample content");
            process.submitApprovalRequest(request); // 提交审批请求给流程
            System.out.println("Request status: " + request.getStatus()); // 打印审批单状态
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建审批流程
        ApprovalProcess process1 = new ApprovalProcess();
        process1.addApprover(new Manager()); // 经理审批
        process1.addApprover(new Supervisor()); // 主管审批

        ApprovalProcess process2 = new ApprovalProcess();
        process2.addApprover(new Supervisor()); // 主管审批
        process2.addApprover(new Manager()); // 经理审批

        // 创建审批流程管理器
        ApprovalProcessManager manager = new ApprovalProcessManager();
        manager.addApprovalProcess(process1);
        manager.addApprovalProcess(process2);

        // 执行审批流程
        manager.executeApprovalProcesses();
    }
}
